--- <action itemid="2559;2558" event="script" value="RPG/small axe and saw.lua"/>
--- Campfire maked by Kronos aka Hellboy ---
local AXE_SAW_CONFIG = {
	tree = {2700, 2701, 2703, 2704, 2705, 2706, 2707, 2708, 2711, 2712, 2719, 2720, 2722, 7024, 8313, 8314, 2709, 2710, 2717},
	stump = 8786,
	effect_stump = CONST_ME_BLOCKHIT,
	
	time_event = 60*60*2 --- seconds
}

local table_ = {
	[2559] = { --- small axe
		[1] = {newId = 2549, effect = CONST_ME_BLOCKHIT}, --- rake
		[8] = {newId = 2512, effect = CONST_ME_BLOCKHIT}, --- wooden shield
		[10] = {newId = 2389, newCount = 2, effect = CONST_ME_BLOCKHIT} --- spear
	},
	[2558] = { --- saw
		[1] = {newId = 7729, effect = CONST_ME_BLOCKHIT}, --- sawdust
		[10] = {newId = 1652, needStack = true, effect = CONST_ME_BLOCKHIT} --- wooden chair
	}
}

function transformBackTree(ID, pos)
	local thing = getTileItemById(pos, AXE_SAW_CONFIG.stump)
	doTransformItem(thing.uid, ID)
end

function onUse(cid, item, fromPosition, itemEx, toPosition)

	if onUseWeapon(cid, item, fromPosition, itemEx, toPosition) then
		return true
	end
	
	if isInArray(AXE_SAW_CONFIG.tree, itemEx.itemid) then --- lumberjacking
		if itemEx.actionid > 0 then
			doSendCancel(cid, 'You cannot cut this tree.')
			return false
		end
		addEvent(transformBackTree, AXE_SAW_CONFIG.time_event*1000, itemEx.itemid, toPosition)
		doTransformItem(itemEx.uid, AXE_SAW_CONFIG.stump)
		doPlayerAddItem(cid, 5901, 1)
		doSendMagicEffect(toPosition, AXE_SAW_CONFIG.effect_stump)
		doCreatureSay(cid, "Ciach!", TALKTYPE_ORANGE_1)
	end
	
	if (itemEx.itemid == 5901) then
		crafting(cid, item, fromPosition, itemEx, toPosition, (table_[item.itemid])[itemEx.type])
	end
	return true
end